package com.situ.crm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Permission;
import com.situ.crm.mapper.PermissionMapper;
import com.situ.crm.service.IPermissionService;
import com.situ.crm.vo.LayUISelectMVO;

@Service
public class PermissionServiceImpl implements IPermissionService {
	@Autowired
	private PermissionMapper permissionMapper;

	@Override
	public ServerResponse pageList(Integer page, Integer limit, Permission permission) {
		//分页插件
		PageHelper.startPage(page,limit);
		//查询
		List<Permission> list = permissionMapper.pageList(permission);
		//封装pageInfo
		PageInfo pageInfo = new PageInfo<>(list);
		//总数据
		Integer count = (int) pageInfo.getTotal();
		return ServerResponse.createSuccess("查找成功", count, list);
	}

	//添加
	@Override
	public ServerResponse insert(Permission permission) {
		try {
			Integer count = permissionMapper.insert(permission);
			if (count == 1) {
				return ServerResponse.createSuccess("添加成功");
			} else {
				return ServerResponse.createError("添加失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("添加失败");
		}
	}

	//id删除
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			Integer count = permissionMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//批量删除
	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArray = ids.split(",");//字符串转换为字符数组 
		try {
			Integer count = permissionMapper.deleteAll(idArray);
			if (count >= 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//修改查询
	@Override
	public Permission selectId(Integer id) {
		return permissionMapper.selectByPrimaryKey(id);
	}

	//修改
	@Override
	public ServerResponse update(Permission permission) {
		try {
			Integer count = permissionMapper.updateByPrimaryKeySelective(permission);
			if (count == 1) {
				return ServerResponse.createSuccess("修改成功");
			} else {
				return ServerResponse.createError("修改失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("修改失败");
		}
	}

	//角色添加查询权限
	@Override
	public ServerResponse selectAllPermission() {
		//查找所有，new Permission()相当于空的，查找所有
		List<Permission> permissions = permissionMapper.pageList(new Permission());
		/*需要的结构 只要这三个数据
		 Integer id;
		 String name;
		 Integer status;*/
		List<LayUISelectMVO> list = new ArrayList<>();
		for (Permission permission : permissions) {
			LayUISelectMVO layUISelectMVO = new LayUISelectMVO();
			layUISelectMVO.setId(permission.getId());
			layUISelectMVO.setName(permission.getName());
			layUISelectMVO.setStatus(1);
			list.add(layUISelectMVO);
		}
		return ServerResponse.createSuccess("权限查找成功ヾ(≧▽≦*)o", list);
	}

	
	
}
